package com.ht.common.core.utils;



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class FileUtil {

	public static String GetTimeToString(int type) {
		SimpleDateFormat sdf = null;
		if (type == 1) {
			sdf = new SimpleDateFormat("yyyyMMddhhmmss");
		} else if (type == 2) {
			sdf = new SimpleDateFormat("yyyy-MM-dd");
		}
		long times = System.currentTimeMillis();
		Date date = new Date(times);
		String s = null;
		if(date!=null) {
			if(sdf!=null) {
				s =  sdf.format(date);
			}
		}
		return s;
	}

	public static void copyFile(List<String> fileList, String newpath) throws IOException {
		if(!fileList.isEmpty()){
			for (String filepath : fileList) {
				 File oldpaths = new File(filepath);
				 File newpaths = new File(newpath + "/" + oldpaths.getName());
				 if(oldpaths.renameTo(newpaths)){
					 oldpaths.delete();
				 }
			}
		}
	} 
	
	/**
	 * 创建目录
	 */
	public static void createDir(String destDirName) {
		String s= FilterPath.getPath(destDirName);
		File dir = new File(s);
		if (!dir.exists()) {
			if (!destDirName.endsWith(File.separator)) {
				destDirName = destDirName + File.separator;
			}
			// 创建目录
			dir.mkdirs();
		}
	}

	/**
	 * 通过递归得到某一路径下所有的文件的全路径,分装到list里面
	 * 
	 * @param filePath
	 * @param filelist
	 * @return
	 */
//	public static List<String> getFiles(String filePath, List<String> filelist,Map<String, MyFile> concurrentHashMap) {
//
//		File root = new File(filePath);
//		if (!root.exists()) {
//			System.out.println(filePath + " 不存在!");
//		} else {
//			//过滤只有BS开头的文件才解析
//			//File[] files = root.listFiles(new KeywordFileFilter("BS"));
//			File[] files  = root.listFiles();
//			Arrays.sort(files, new FileUtil.CompratorByLastModified());
//
//			for (File file : files) {
//				if (file.isDirectory()) {
//					// getFiles(file.getAbsolutePath(), filelist);
//				} else {
//					// logger.info("目录:" + filePath + "文件全路径:" +
//					// file.getAbsolutePath());
//					if(!concurrentHashMap.containsKey(file.getName())) {
//						filelist.add(file.getAbsolutePath());
//					}
//				}
//			}
//		}
//		return filelist;
//	}
	/**
	 * 通过递归得到某一路径下所有的文件的全路径,分装到list里面
	 * 
	 * @param filePath
	 * @param filelist
	 * @return
	 */
	public static List<String> getFiles(String filePath, List<String> filelist) {
		File root = new File(filePath);
		if (!root.exists()) {
			System.out.println(filePath + " 不存在!");
		} else {
			File[] files = root.listFiles();
			Arrays.sort(files, new CompratorByLastModified());
			for (File file : files) {
				if (file.isDirectory()) {
				} else {
					filelist.add(file.getAbsolutePath());
				}
			}
		}
		return filelist;
	}

	// 根据文件修改时间进行比较的内部类
	static class CompratorByLastModified implements Comparator<File> {

		public int compare(File f1, File f2) {
			long diff = f1.lastModified() - f2.lastModified();
			if (diff > 0) {
				return 1;
			} else if (diff == 0) {
				return 0;
			} else {
				return -1;
			}
		}
	}

	public static boolean copyFile(File fin, File fout) throws IOException {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		boolean result;

		byte[] b = new byte[512 * 1024];
		int len;
		try {
			fis = new FileInputStream(fin);
			// 判断输出文件目录是否存在，如果没有，则创建目录
			checkDirectory(fout.getParent());
			fos = new FileOutputStream(fout);
			while ((len = fis.read(b)) != -1) {
				fos.write(b, 0, len);
			}
			fos.flush();
			fis.close();
			fos.close();

			result = true;
		} catch (IOException e) {
			try {
				if (fis != null)
					fis.close();
				if (fos != null) {
					fos.flush();
					fos.close();
				}
				if (fis != null)
					fis.close();

			} catch (IOException ex) {
			}
			// logger.info("Cann't Copy File: " +
			// e.getMessage()+"====fin="+fin.getPath()+"======fout="+fout.getPath());
			result = false;
			throw e;
		}  finally {
			if(fos!=null){
				fos.close();
			}if(fis!=null){
				fis.close();
			}
		}


		return result;
	}

	/**
	 * 检测指定路径是否存在，不存在则创建
	 * 
	 * @param directory
	 *            指定路径
	 */
	public static void checkDirectory(String directory) {
		// 建立多个文件夹
		File file = new File(directory);

		if (!file.exists()) {
			file.mkdirs();
		}
	}
	
	/**
	 * 获得文件名的后缀名
	 * @param fileName
	 * @return
	 */
	public static String getSuffixName(String fileName){
		String suffix=null;
		if(StringUtils.isBlank(fileName)){
			return suffix;
		}
		if(-1==fileName.indexOf(".")){
			return suffix;
		}
		if(-1==fileName.indexOf(".")){
			return suffix;
		}
		suffix=fileName.substring(fileName.lastIndexOf(".")+1);
		if(StringUtils.isBlank(suffix)){
			suffix=null;
		}
		return suffix;
	}
	
	public static void main(String[] args) {
		System.out.println(getSuffixName(".jpg"));
		System.out.println(getSuffixName("jj.jpg"));
		System.out.println(getSuffixName("jj."));
		System.out.println(getSuffixName("."));
		System.out.println(getSuffixName(""));
		System.out.println(getSuffixName("1511554"));
		System.out.println(getSuffixName("  "));
	}

}
